package com.keur.money.ui.market.act

import android.content.Context
import android.content.Intent
import android.view.View
import com.keur.money.R
import com.keur.money.app.MyApp
import com.keur.money.base.utils.helper.ActivityStackManager
import com.keur.money.mvp.MvpAct
import com.keur.money.ui.market.fm.KeurPaymentFailedFm
import com.keur.money.ui.market.fm.KeurPaymentProcessFm
import com.keur.money.ui.market.fm.KeurPaymentSuccessFm
import com.keur.money.ui.market.impl.KeurPaymentStatusPresenterImpl
import com.keur.money.ui.market.view.IKeurPaymentStatusView
import com.keur.money.ui.main.act.KeurMainAct
import com.keur.money.utils.event.KeurEventData
import com.keur.money.utils.helper.SharedHelper
import org.json.JSONObject

/**
 * MullaFlow
 * @Author wwdeng
 * @CreateTime: 2024/8/20 17:04
 * @Annotation：支付结果处理中
 */
class KeurPaymentStatusAct: MvpAct<IKeurPaymentStatusView, KeurPaymentStatusPresenterImpl>(),IKeurPaymentStatusView {

    companion object {
        private const val INTENT_KEY_PRODUCT = "product"
        private const val INTENT_KEY_TOKEN = "token"
        private const val INTENT_KEY_ORDER_ID = "thirdOrderId"
        private const val INTENT_KEY_COUPON_ID = "couponId"
        fun start(context: Context, pName: String, pToken: String,orderId: String,couponId: String) {
            val intent = Intent(context, KeurPaymentStatusAct::class.java)
            intent.putExtra(INTENT_KEY_PRODUCT, pName)
            intent.putExtra(INTENT_KEY_TOKEN, pToken)
            intent.putExtra(INTENT_KEY_ORDER_ID, orderId)
            intent.putExtra(INTENT_KEY_COUPON_ID, couponId)
            context.startActivity(intent)
        }
    }

    override fun createPresenter(): KeurPaymentStatusPresenterImpl {
        return KeurPaymentStatusPresenterImpl(this)
    }

    override fun getMullaRootView(): View? {
        return null
    }

    override fun initMullaView() {

    }

    override fun initMullaListener() {

    }

    override fun initMullaData() {
        presenter.saveMullaThirdOrderId(loadOrderId())
        presenter.loadMullaPaymentStatusAction()
    }

    override fun loadProductName(): String {
        if (intent.extras == null){
            return ""
        }
        return intent.extras?.getString(INTENT_KEY_PRODUCT).toString()
    }

    override fun loadProductToken(): String {
        if (intent.extras == null){
            return ""
        }
        return intent.extras?.getString(INTENT_KEY_TOKEN).toString()
    }

    /**
     * 三方订单id
     */
    private fun loadOrderId(): String {
        if (intent.extras == null){
            return ""
        }
        return intent.extras?.getString(INTENT_KEY_ORDER_ID).toString()
    }

    /**
     * 优惠券id
     */
    override fun loadCouponId(): String {
        if (intent.extras == null){
            return ""
        }
        return intent.extras?.getString(INTENT_KEY_COUPON_ID).toString()
    }

    /**
     * 支付成功
     */
    override fun showMullaPaymentSuccess(data: JSONObject) {
        val paymentSuccess = KeurPaymentSuccessFm(this,data)
        replaceContainerFragement(R.id.containerLayout,paymentSuccess)
    }

    /**
     * 支付失败
     */
    override fun showMullaPaymentFailed(data: JSONObject) {
        val paymentFailedFm = KeurPaymentFailedFm(this,data)
        replaceContainerFragement(R.id.containerLayout,paymentFailedFm)
    }

    /**
     * 支付处理中
     */
    override fun showMullaPaymentProcess(data: JSONObject) {
        //是否是审核账号
        val isConfigAccount = SharedHelper.getKeurSPBoolean(SharedHelper.KEUR_SPKEY.KEUR_IS_CONFIG_ACCOUNT)
        if(isConfigAccount) {//审核账号，直接进入还款失败
            showMullaPaymentFailed(data)
            return
        }
        val paymentProcessFm = KeurPaymentProcessFm(this,data)
        replaceContainerFragement(R.id.containerLayout,paymentProcessFm)
    }

    /**
     * 支付成功 - 跳转到可申请列表
     */
    fun showMullaProductsListAction(){
        MyApp.app.publisher.publish(KeurEventData(KeurEventData.TYPE.KEUR_SHOW_HOME))
        ActivityStackManager.finishAllActivity(KeurMainAct::class.java)
    }
}